<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 群聊内容
 *
 * @icon fa fa-circle-o
 */
class Chatgroupcontent extends Backend
{

    /**
     * Chatgroupcontent模型对象
     * @var \app\admin\model\Chatgroupcontent
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Chatgroupcontent;
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $ids=input('ids/d',0);
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','group'])
                    ->where($where)
                    ->where(" FIND_IN_SET($ids,room_ids)")
                    ->where('chatgroupcontent.type','in','1,2,3')
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['username','tel']);
				$row->getRelation('group')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig("ids", $ids);
        return $this->view->fetch();
    }
     //导出
     public function export()
     {
        $ids=input('ids/d',0);
        // var_dump($ids);die;
          //当前是否为关联查询
        $search = $this->request->post('search');
        $idss = $this->request->post('idss');
        $filter = $this->request->post('filter');
        $op = $this->request->post('op');
        $columns = $this->request->post('columns');
        $this->request->get(['search' => $search, 'ids' => $idss, 'filter' => $filter, 'op' => $op]);
        //设置过滤方法
        
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        
            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->where(" FIND_IN_SET($ids,room_ids)")
                    ->order($sort, $order)
                    ->select();
            // var_dump($where);die;
            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['username','tel']);
				// $row->getRelation('group')->visible(['name']);
            }
            // echo $this->model->getlastsql();
            // var_dump($list);die;
            
        
         // die;
              // $excel = new Excel();
             vendor('PHPExcel.PHPExcel');
             //添加标题到数组头部
             $file_name = '聊天列表' . date('Y-m-d_His') . '.xls';
             $PHPExcel = new \PHPExcel();
             // print_r($PHPExcel);die;
             
             $PHPSheet = $PHPExcel->getActiveSheet();
             // 设置A列的宽度为20
            $PHPSheet->getColumnDimension('A')->setWidth(30);
            // 设置B列的宽度为30
            $PHPSheet->getColumnDimension('B')->setWidth(30);
            $PHPSheet->getColumnDimension('C')->setWidth(100);
             $PHPSheet->setTitle("表格一");
             $PHPSheet->setCellValue("A1", "聊天人昵称");
             $PHPSheet->setCellValue("B1", "手机号");
             $PHPSheet->setCellValue("C1", "内容");
             $PHPSheet->setCellValue("D1", "类型");
             $PHPSheet->setCellValue("E1", "发布时间");
             $i = 2;
            // dump($res);die;
             foreach ($list as $keys => $value) {  
                $typestr='';
                if($value['type']=='1')
                {
                    $typestr='文字';
                }  
                if($value['type']=='2')
                {
                    $typestr='图片';
                } 
                if($value['type']=='3')
                {
                    $typestr='视频';
                } 
                 $PHPSheet->setCellValue('A' . $i, '' . $value->user->username);
                 $PHPSheet->setCellValue('B' . $i, '' . $value->user->tel);
                 $PHPSheet->setCellValue('C' . $i, '' . $value['content']);
                 $PHPSheet->setCellValue('D' . $i, '' . $typestr);
                 $PHPSheet->setCellValue('E' . $i, '' . date('Y-m-d H:i:s',$value['add_time']));
                 
                 $i++;
             }
            
             $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
             header('Content-Disposition: attachment;filename=' . $file_name);
             header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
             $PHPWriter->save("php://output");
             
        
        
     }

}
